﻿; (function ($) {
/*
 
 Show Product Stock Infos

*/
$.fn.showProductStockInfos = function (options) {
	if (this.length > 1) {
		this.each(function()  {
				var elm = $(this);
				elm.showProductStockInfos(options)
			}
		);
		return this;
	}

	var settings = {};
	var element = null;

	this.init = function() {

		element = $(this);
		settings = $.extend({} , $.fn.showProductStockInfos.defaultOptions, options);

		showProductStockInfos();

		return this;
	}

	function showProductStockInfos() {
		$(element).empty();
		var productCode = $(element).attr('rel');
		var parts = productCode.split('|');
		var viewName = settings.viewName;
		if (parts.length > 1) {
			productCode = parts[0];
			viewName = parts[1];
		}

		$.ajax({
			type : 'POST',
			contentType : 'application/json',
			url : settings.url,
			data : JSON.stringify({ 
						viewName : viewName ,
						productCode : productCode
					}),
			success : function (html) {
				$(element).html(html);
			}
		});
	}

	return this.init();
};

$.fn.showProductStockInfos.defaultOptions = {
	url: null,
	viewName: null
};

})(jQuery);

function Product(data) {
	var self = this;

	self.code = ko.observable(data.Code);
	self.title = ko.observable(data.Title);
	self.imageSrc = ko.observable(data.DefaultImage.Url);
	self.packaging = ko.observable(data.Packaging.Value);
	self.saleUnitValue = ko.observable(data.SaleUnitValue);
	self.shortDescription = ko.observable(data.ShortDescription);
	self.href = ko.observable();
}

function StockInfo(data) {
	var self = this;
	self.deliveryDaysCount = ko.observable(data.DeliveryDaysCount);
	self.minimalQuantity = ko.observable(data.MinimalQuantity);
	self.mostProvisionningDate = ko.observable(data.MostProvisionningDate);
	self.physicalStock = ko.observable(data.PhysicalStock);
	self.provisionnedQuantity = ko.observable(data.ProvisionnedQuantity);
	self.provisionningDaysCount = ko.observable(data.ProvisionningDaysCount);
	self.reservedQuantity = ko.observable(data.ReservedQuantity);
	self.hasStock = ko.computed(function () {
		return self.physicalStock() > 0;
	});
}

